package fxy.fxyOffer;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

public class HashTableTest {
	public static void main(String[] args) {
		HashTableTest.testHashtableAPIs();
	}
	private static void testHashtableAPIs() {
		         // ��ʼ���������
		         Random r = new Random();
		         // �½�Hashtable
		         Hashtable<String,Object> table = new Hashtable<String,Object>();
		        // ��Ӳ���
		         table.put("one", r.nextInt(10));
		         table.put("two", r.nextInt(10));
		         table.put("three", r.nextInt(10));
		 
		         // ��ӡ��table
		         System.out.println("table:"+table );
		 
		         System.out.print("(1)ͨ��Iterator����key-value: ");
		         // ͨ��Iterator����key-value
//		        Iterator iter = table.entrySet().iterator();
//		         while(iter.hasNext()) {
//		             Map.Entry entry = (Map.Entry)iter.next();
//		             System.out.println("next : "+ entry.getKey() +" - "+entry.getValue());
//		         }
		         Iterator<Map.Entry<String, Object>> iter = table.entrySet().iterator();
		         while(iter.hasNext()){
		        	 Map.Entry<String, Object> iterEntry = iter.next();
		        	 System.out.println("next : "+ iterEntry.getKey() +" - "+iterEntry.getValue());
		         }
		         System.out.print("(2)ͨ��Iterator����key-value: ");
		         Iterator<Map.Entry<String, Object>> iter1 = table.entrySet().iterator();
		         while(iter1.hasNext()){
		        	 Map.Entry<String, Object> iterEntry = iter1.next();
		        	 System.out.println("next : "+ iterEntry.getKey() +" - "+table.get(iterEntry.getKey()));
		         }
		         System.out.print("(3)ͨ��hashtable��keys����key-value: ");
		         Enumeration<String> keys = table.keys();
		         while(keys.hasMoreElements()){
		        	 String key = keys.nextElement();
		        	 System.out.println("next : "+ key +" - "+table.get(key));
		         }
		         System.out.print("(4)ͨ��hashtable��keys����key-value: ");
		         table.values();
		         Enumeration o = table.elements();
		         while(o.hasMoreElements()){
		        	 Object key =  o.nextElement();
		        	 System.out.println("next : "+ key +" - ");
		         }
		        // Hashtable�ļ�ֵ�Ը���        
		         System.out.println("size:"+table.size());
		         
		         // containsKey(Object key) :�Ƿ������key
		         System.out.println("contains key two : "+table.containsKey("two"));
		         System.out.println("contains key five : "+table.containsKey("five"));
		 
		         // containsValue(Object value) :�Ƿ����ֵvalue
		         System.out.println("contains value 0 : "+table.containsValue(new Integer(0)));
		 
		         // remove(Object key) �� ɾ����key��Ӧ�ļ�ֵ��
		         table.remove("three");
		 
		         System.out.println("table:"+table );
		 
		         // clear() �� ���Hashtable
		         table.clear();
		 
		         // isEmpty() : Hashtable�Ƿ�Ϊ��
		         System.out.println((table.isEmpty()?"table is empty":"table is not empty") );
		     }
}
